package org.example.myframework.frame.common.tool;

import java.io.File;
import java.io.FileInputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class PropertiesUtils {


    private Map<String, Properties> propertiesMap = new HashMap<String, Properties>();

    private Map<String, Long> modifyTimeMap = new HashMap<>();

    private String configPath = "";

    public void setConfigPath(String path) {
        this.configPath = path;
    }

    // 单例模式，私有化构造方法，防止被外界私有化
    private PropertiesUtils(){}

    public static class SingleHolder {
        private static PropertiesUtils instance = new PropertiesUtils();
    }

    // 对外提供获取当前实例的静态方法
    public static PropertiesUtils getInstance() {
        // 整个生命周期内，都只有这一个实例化对象
        return SingleHolder.instance;
    }

    public String getPropertyValue(String properFileName, String key) throws Exception{
        String fileName = convertFileName(properFileName);
        // map中没有获取到
        if (propertiesMap.get(fileName) == null) {
            loadProperties(fileName);
        } else {
            checkPropertiesModified(fileName);
        }
        return propertiesMap.get(fileName).getProperty(key);
    }

    private void checkPropertiesModified(String fileName) throws Exception{
        File file = getPropertiesFile(fileName);;
        long newTime = file.lastModified();
        Long lastUpdateTime = modifyTimeMap.get(fileName);
        if (newTime != lastUpdateTime) {
            loadProperties(fileName);
        }
    }

    /**
     * 将参数直接加载到Map中，方便我们直接去读取
     * @param fileName
     */
    private void loadProperties(String fileName) throws Exception {
        // 获取File文件
        File file = getPropertiesFile(fileName);;
        Long newTime = file.lastModified();
        if (propertiesMap.get(fileName) != null) {
            propertiesMap.remove(fileName);
        }
        Properties properties = new Properties();
        properties.load(new FileInputStream(file));
        propertiesMap.put(fileName, properties);
        modifyTimeMap.put(fileName, newTime);
    }

    private File getPropertiesFile(String fileName) throws Exception {
        File propertiesFile = null;
        if (this.configPath != null && !this.configPath.equals("")) {
            return new File(this.configPath + File.separator + fileName + ".properties");
        }
        String dir = System.getProperty("user.dir") + File.separator + fileName + ".properties";
        propertiesFile = new File(dir);
        if (!propertiesFile.exists()) {
            URL url = PropertiesUtils.class.getResource("/" + fileName + ".properties");
            if (url != null) {
                propertiesFile = new File(url.getFile());
            }
        }
        return propertiesFile;
    }

    private String convertFileName(String filename) {
        // 一个例子
        if (!filename.endsWith(".properties")) {
            return filename;
        }
        int index = filename.lastIndexOf(".");
        filename = filename.substring(0, index);
        return filename;
    }

}
